from flask import Flask, request, send_from_directory,render_template
import mysqldb_server
import os
path = os.path.dirname(__file__)
app = Flask(__name__, static_folder=os.path.join(path, "static"), template_folder=os.path.join(path, "templates"))


@app.route('/', methods=['POST', 'GET'])
def rec_index():
    return render_template("index.html")
    # return send_from_directory(root,"index.html")


@app.route('/reg', methods=['POST', 'GET'])
def rec_reg():
    return render_template("regiretc.html")

@app.route('/check', methods=['POST', 'GET'])
def rec_uap():
    usr = request.form['username']
    passwd = request.form['passwd']
    res = mysqldb_server.check_login_db(usr, passwd)
    return res


# 添加好友
@app.route('/add_friend', methods=['POST', 'GET'])
def add_one():
    usr = request.form['username']
    nicheng = request.form['nicheng']
    friend_username = request.form['friend_name']
    print(usr, nicheng, friend_username)
    res = mysqldb_server.add_friend(usr,nicheng,friend_username)
    print(res)
    return res

# 注册
@app.route("/register", methods=['POST','GET'])
def register_id():
    usr = request.form['name']
    nicheng = request.form['nicheng']
    passwd = request.form['passwd']
    geqian = request.form['geqian']
    print(usr, nicheng, passwd, geqian)
    res = mysqldb_server.create_new_id(usr, nicheng, passwd, geqian)
    if res == "该账号已存在":
        return "<h2>{}:该账户已经存在<h2>".format(usr)
    else:
        return "<h2>恭喜你注册成功，请下载app登陆<h2>"
    # return redirect(url_for(res))


# 获取好友列表
@app.route('/get_friends_list', methods=['POST','GET'])
def get_all():
    usr = request.form['username']
    res = mysqldb_server.get_all_friends(usr)
    return res

# 测试网页数据
@app.route('/hh', methods=['POST','GET'])
def welcome():
    return "helloword"


@app.route('/chatlog',methods=['POST','GET'])
def chatlog():
    username = request.form['username']
    friend_name = request.form['friendname']
    res = mysqldb_server.chatlog(username,friend_name)
    # print(str(res))
    return str(res)


# 首页测试



def main():
    app.run(host='127.0.0.1', port=4000,debug=True)


if __name__ == '__main__':
    main()

    # app.run(host='192.168.31.99', port=4000)
